package ui.views;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.listener.ViewActivateEvent;
import ui.provider.MarketVariableContent;
import ui.provider.MarketVariableLabel;
import ui.util.IPartId;

public class MarketVariableView extends ViewPart  
			implements	ITabbedPropertySheetPageContributor {
	
	public final static Logger logger = LoggerFactory.getLogger(MarketVariableView.class);

	private TreeViewer treeViewer;
	private Composite myControl;

	public void createPartControl(Composite parent) {

		treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);
		// treeViewer = new CheckboxTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL|
		// SWT.V_SCROLL);
		treeViewer.setContentProvider(new MarketVariableContent());
		treeViewer.setLabelProvider(new MarketVariableLabel());
		// Expand the tree
		treeViewer.setAutoExpandLevel(1);
		// Provide the input to the ContentProvider, every class except null
		treeViewer.setInput(treeViewer);

		// filter Setting
		// treeViewer.setFilters(new ViewerFilter[]{new
		// HistoricalScenarioFilter()});
		// Tree Background color
		// treeViewer.getTree().setBackground(blue);

		// Selection Provider
		getSite().setSelectionProvider(treeViewer);
		
//		Add Event
		treeViewer.addPostSelectionChangedListener(new ViewActivateEvent(getSite().getPage(), IPartId.MarketVariable,IPartId.MvProp));

		hookDoubleClickCommand();

		// createFiltersAndSorters();
		// createActions();
		// createMenus();
		// createToolbar();
		// createContextMenu();

	}

	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}

	// For Tabbed Property
//	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class){
			logger.debug("getAdapter in MarketVariableView:{}" );
			return new TabbedPropertySheetPage(this);
		}
//		logger.debug("getAdapter :{}" );
		return super.getAdapter(adapter);
	}
	@Override
	public String getContributorId() {
//		logger.debug("contributor Id:{}" , getSite().getId());
		return getSite().getId();
	}

	private void hookDoubleClickCommand() {
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IHandlerService handlerService = (IHandlerService) getSite()
						.getService(IHandlerService.class);
				try {
					System.out.println("uztest 19 : before the Execution");
					handlerService.executeCommand(
							"com.eugene.ui.commands.CallIrCurveEditor", null);
				} catch (Exception ex) {
					// throw new
					// RuntimeException("CallIrCurveEditor not found");
					// MessageDialog.openWarning(getSite().getShell(),"wrong","no");

				}
			}
		});
	}

}
